<?php /*a:1:{s:70:"D:\web\wwwroot\tp5.1\public/../app/admin\view\welfare\showwelfare.html";i:1528443945;}*/ ?>
<!DOCTYPE html>
<html>
    <head>
        <meta charset="utf-8"/>
        <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1"/>
        <title>
            搜索物品
        </title>
        <meta name="description" content=""/>
        <meta name="keywords" content=""/>
        <link rel="stylesheet" href="/static/frame/layui/css/layui.css"/>
        <link rel="stylesheet" href="/static/frame/static/css/style.css"/>
        <style type="text/css">
    .layui-btn-group{margin-top:10px;margin-right:10px;}
.layui-btn-group+.layui-btn-group{margin-left:0;margin-right:10px;}
</style>
    </head>
    <body class="body">
        <div class="layui-row">
            <form class="layui-form" action="" id="editform">
                <div class="layui-inline">
                    <input class="layui-input layui-input-sm" name="keywords" placeholder="请输入名称" id="search-elm" autocomplete="off" value="<?php echo htmlentities($keywords); ?>" />
                </div>
                <button class="layui-btn site-demo-active layui-btn-sm search-btn" lay-submit="" lay-filter="demo1" >搜索</button>
            </form>
        </div>
        <br />
        <div class="layui-row">
	         <div class="layui-col-xs8 layui-col-sm7 layui-col-md8">
	        	<div class="goodshtm" id="goodshtm"></div>
	        </div>
	         <div class="layui-col-xs4 layui-col-sm5 layui-col-md4">
				<div id="transmit" class="layui-btn layui-btn-primary layui-btn-sm">确定</div>
			</div>
        </div>
        <div class="goods" id="goods" style="display: none"></div>
        <div class="layui-row">
            <table class="layui-table">
                <thead>
                    <tr><th>id</th><th>名称</th><th>数量</th><th>操作</th></tr>
                </thead>
                <tbody id="table-search"></tbody>
            </table>
        </div>



<script type="text/javascript" src="/static/frame/layui/layui.js"></script>
<script src="/static/admin/js/comm.js" charset="utf-8"></script>
<!-- 注意：如果你直接复制所有代码到本地，上述js路径需要改成你本地的 -->
<script>
layui.use(['form', 'table', 'admin'], function() {
    var $ = layui.jquery,
        form = layui.form,
        admin = layui.admin;


        form.on('submit(demo1)', function(data){
			var html = '';
			$.post("<?php echo url('ajaxkeywords'); ?>", $("#editform").serialize(), function(data) {
				if(data.code == 1){
					$.each(data.data,function(index, el) {
						html += '<tr><td class="data-id">'+el.wid+'</td><td class="data-name">'+el.title+'</td><td><input type="number" name="num" class="layui-input" lay-verify="required" min="1" placeholder="数量" value="1" style="width:100px"></td><td><i class="layui-icon layui-icon-add-1 search-add"></i></td></tr>';
					});
					 $("#table-search").html('');
					 $("#table-search").append(html);
                   }

                });
			return false;
        });


		var index = parent.layer.getFrameIndex(window.name); //获取窗口索引

		//给父页面传值
		$('#transmit').on('click', function(){
			$("textarea[name='goods']", parent.document).val($('#goods').text())
			//parent.$("#sfsfsdfsd").val($('#goods').text());
			//parent.layer.msg('您将标记 [ ' + $('#goods').text() + ' ] 成功传送给了父窗口');
		    parent.layer.close(index);
		});

       //$('.search-add').unbind('click').click(function(event) {
       $("body").on("click", ".search-add", function() {
       		that = $(this);
       		var goodshtm = '';
       		var goods = $('#goods').text();
       		var wid = that.parents('tr').find('.data-id').text(); 
       		var wname = that.parents('tr').find('.data-name').text(); 
       		var wnum = that.parents('tr').find('input[name=num]').val(); 
       		if(goods.split("|").length > 9){
       			layer.msg('同时添加物品不要超过9类');
       			return;
       		}
       		if(goods.length>0){
       			var goodsarr = goods.split("|");
       			var nostart = false;
       			for (var i = goodsarr.length - 1; i >= 0; i--) {
       				if(goodsarr[i].split(",")[0] == wid && goodsarr[i].split(",")[1] == wname){
       					var sss = parseInt(goodsarr[i].split(",")[2]) + parseInt(wnum);
       					goodsarr[i] = goodsarr[i].split(",")[0]+','+goodsarr[i].split(",")[1]+','+sss;
       					nostart = true;
       				}
       			}
       			if(nostart){
       				goods = goodsarr.join('|');
       			}else{
       				goods = goods+'|'+wid+','+wname+','+wnum;
       			}
       		}else{
       			goods = wid+','+wname+','+wnum;
       		}

       		for (var i = 0 ;i< goods.split("|").length; i++) {
       			goodshtm += '<div class="layui-btn-group"><a class="layui-btn layui-btn-primary layui-btn-sm">'+ goods.split("|")[i] + '</a><a class="layui-btn layui-btn-sm layui-icon layui-btn-primary layui-icon-close clickclose"></a></div>';
       		}

       		$('#goodshtm').html(goodshtm);
       		$('#goods').text(goods);
       });

		$("body").on("click", ".clickclose", function() {
			var nhtm = $(this).parent().find('.layui-btn-sm').text();
			$(this).parent().remove();
			var goods = $('#goods').text();
			var goodsarr = goods.split("|");
       		var nostart = false;
       		for (var i = goodsarr.length - 1; i >= 0; i--) {
       			if(goodsarr[i] == nhtm){
					goodsarr.splice(i, 1);
       			}
       		}
       		goods = goodsarr.join('|');
       		$('#goods').text(goods);
		});





});

</script>

    </body>
</html>
